<?php
namespace app\admin\model;

use think\Model;  
use think\Loader;
use think\Validate;
use think\Db;
class BannerM  extends Model
{

  protected  $pk='bitem_id';//主键
  protected $table = 'xcx_banner_item';
    public function add_bannerM($data)
    {
    	//halt($data);
    	//yanzheng
    	$validate = Loader::validate('BannerM');
    	if(!$validate->check($data)){
		    return ['valid'=>0,'msg'=>$validate->getError()];
		}
		$http='http';
		$img['form']=0;
		$img['img_url']=$data['bannerm_img'];
		if($data['action_type'])
		{

		    if(strpos($data['action_type'],'t')===false)
		    {

                $p=substr($data['action_type'],1);
		        $arr['url_type']='2';
                $arr['action_url']='goods_detail?id='.$p;
            }elseif(strpos($data['action_type'],'p')===false)
            {

                $p=substr($data['action_type'],1);
                $arr['url_type']='1';
                $arr['action_url']='list_recommend?id='.$p;
            }
        }else
            {

                $arr['url_type']='0';
                $arr['action_url']='';
            }
         $arr['url_action_type']=$data['url_action_type'];
		if(stristr($img['img_url'],$http))
		{
			$img['form']=1;
		}
		$img['createtime']=time();
		$img['img_states']=1;
		$getImgId=db('bannerimg')->insertGetId($img);
        $arr['banimg_id']=$getImgId;
		$arr['bitem_name']=$data['bannerm_name'];
		/*判断跳转到哪*/
	    $arr['banner_id']=$data['banner_id'];
	    $arr['bitem_sort']=$data['bannerm_sort'];
	    $arr['bitem_states']=1;
	    $arr['createtime']=time();

		//判断名称是否唯一
		$isNotRepeat=$this->where('bitem_name',$data['bannerm_name'])->find();
		//halt($type_yan);
		if($isNotRepeat['bitem_id']!="")
		{
			 return ['valid'=>0,'msg'=>'名称重复'];
			 exit;
		}

		//halt($user_yan);
		$result = $this->save($arr);
		if(false === $result){
		    // 验证失败 输出错误信息
		    return ['valid'=>1,'msg'=>'添加失败'];
		}else{
			//添加操作记录
			return ['valid'=>4,'msg'=>'添加成功'];
		}
    }
    public function edit_banner($data)
    {
        //判断图片信息是否修改

        unset($data['urlRoute']);
        if($data['action_type'])
        {

            if(strpos($data['action_type'],'t')===false)
            {

                $p=substr($data['action_type'],1);
                $data['url_type']='2';
                $data['action_url']='goods_detail?id='.$p;
            }elseif(strpos($data['action_type'],'p')===false)
            {

                $p=substr($data['action_type'],1);
                $data['url_type']='1';
                $data['action_url']='list_recommend?id='.$p;
            }
        }else
        {

            $data['url_type']='0';
            $data['action_url']='';
        }
        unset($data['action_type']);
        $img_id=$this->where('bitem_id',$data['bitem_id'])->value('banimg_id');
        $imgSrc=db('bannerimg')->where('banimg_id',$img_id)->value('img_url');
        if($imgSrc==$data['bannerm_img'])
        {
            unset($data['bannerm_img']);
        }else
            {
                $arr['img_url']=$data['bannerm_img'];
                $arr['createtime']=time();
                $id=db('bannerimg')->insertGetId($arr);
                $data['banimg_id']=$id;
                unset($data['bannerm_img']);
            }
        $result=$this->save($data,['bitem_id'=>$data['bitem_id']]);
        if(false === $result){
            // 验证失败 输出错误信息
            return ['valid'=>1,'msg'=>'修改失败'];
        }else{
            //添加操作记录
            return ['valid'=>4,'msg'=>'修改成功'];
        }
    }
    //下架
    public function shelf($data)
    {
        $pspeci_states=$this->where('bitem_id',$data['id'])->value('bitem_states');

        if($pspeci_states==1)
        {
            $result=$this->where('bitem_id',$data['id'])->update(['bitem_states'=>0]);
        }else
        {
            $result=$this->where('bitem_id',$data['id'])->update(['bitem_states'=>1]);
        }
        if(false === $result){
            // 验证失败 输出错误信息
            return ['valid'=>1,'msg'=>'操作失败'];
        }else{
            return ['valid'=>4,'msg'=>'操作成功'];
        }
    }
}   	